#include "dialog_io_config.h"

DialogIoConfig::DialogIoConfig(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	this->setWindowFlags(Qt::Dialog);
	craft_data = CraftData::getInstance();
	updataUI(0);
	last_index = 0;
	connect(ui.cb_type,SIGNAL(currentIndexChanged(int)),this,SLOT(changeIndex(int)));										

}

DialogIoConfig::~DialogIoConfig()
{
	saveDataFromUI(last_index);
}


void DialogIoConfig::updataUI( int index )
{
	ui.cb_enable_1->setChecked(craft_data->config_data->ioConfig[index].b_enable);
	ui.cb_enable_2->setChecked(craft_data->config_data->ioConfig[8+index].b_enable);

	ui.sb_module_1->setValue(craft_data->config_data->ioConfig[index].module_num);
	ui.sb_module_2->setValue(craft_data->config_data->ioConfig[8+index].module_num);

	ui.sb_bit_1->setValue(craft_data->config_data->ioConfig[index].bit_num);
	ui.sb_bit_2->setValue(craft_data->config_data->ioConfig[8+index].bit_num);

}


void DialogIoConfig::saveDataFromUI( int index )
{
	craft_data->config_data->ioConfig[index].b_enable = ui.cb_enable_1->isChecked();
	craft_data->config_data->ioConfig[8+index].b_enable = ui.cb_enable_2->isChecked();

	craft_data->config_data->ioConfig[index].module_num = ui.sb_module_1->value();
	craft_data->config_data->ioConfig[8+index].module_num = ui.sb_module_2->value();

	craft_data->config_data->ioConfig[index].bit_num = ui.sb_bit_1->value();
	craft_data->config_data->ioConfig[8+index].bit_num = ui.sb_bit_2->value();

}

void DialogIoConfig::changeIndex( int index )
{
	saveDataFromUI(last_index);
	updataUI(index);
	last_index = index;
}
